﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.WeChatComponent.WeChatMessagePushRecord;
using BCDto.WeChatComponent.WeChatPushMessage;
using BCService.WeChatComponent.WeChatMessagePushService;

namespace BCTMSRESTful.Controllers.Management.WeChat
{
    /// <summary>
    /// 微信图文控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class WeChatPushMessageController : ManagementBaseController
    {
        private readonly IWeChatMessagePushService weChatMessagePushService;

        public WeChatPushMessageController(IWeChatMessagePushService weChatMessagePushService)
        {
            this.weChatMessagePushService = weChatMessagePushService;
        }

        /// <summary>
        /// 预览微信群发。
        /// </summary>
        /// <param name="mediaId">微信图文文章内容列表</param>
        /// <param name="wxName">微信号</param>
        /// <param name="content">文本群发时需要填写content</param>
        /// <response code="201">已添加成功的微信图文文章信息。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(string), 200)]
        public IActionResult PushPreview([FromQuery(Name = "media_id")]string mediaId, [FromQuery(Name = "wx_name")]string wxName, [FromQuery(Name = "content")]string content)
        {
            return this.Ok(this.weChatMessagePushService.PreviewPushMessage(mediaId, null, wxName, content));
        }

        /// <summary>
        /// 微信群发信息。
        /// </summary>
        /// <param name="weChatPushMessageRequestDto">微信群发信息</param>
        /// <response code="200">发送群发结果</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult PushMessage([FromBody]WeChatPushMessageRequestDto weChatPushMessageRequestDto)
        {
            this.weChatMessagePushService.PushMessage(weChatPushMessageRequestDto);
            return this.Ok(true);
        }

        /// <summary>
        /// 删除微信群发信息。
        /// </summary>
        /// <param name="weChatPushMessageDeleteDto">删除微信群发信息</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpPut("[action]")]
        public IActionResult RemovePushMessage([FromBody]WeChatPushMessageDeleteDto weChatPushMessageDeleteDto)
        {
            this.weChatMessagePushService.DeletePushMessage(weChatPushMessageDeleteDto);
            return this.NoContent();
        }
        /// <summary>
        /// 获取微信群发记录列表(分页)。
        /// </summary>
        /// <param name="mediaId">素材id</param>
        /// <param name="msgStatus">消息状态</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">获取微信自定义菜单列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<WeChatMessagePushRecordDto>), 200)]
        public IActionResult Query([FromQuery(Name = "media_id")]string mediaId,
            [FromQuery(Name = "msg_status")]string msgStatus,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.weChatMessagePushService.Query(mediaId, msgStatus, start, end, ascending, index, size));
        }

        /// <summary>
        /// 设置微信群发文章评论开关功能。
        /// </summary>
        /// <param name="mediaId">图文素材Id</param>
        /// <param name="messageDataId">群发成功后的消息Id</param>
        /// <param name="index">要操作的文章索引</param>
        /// <param name="openComment">打开或关闭评论</param>
        /// <response code="200">发送群发结果</response>
        /// <returns></returns>
        [HttpPut("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult CommentSettings([FromQuery(Name ="media_id")]string mediaId,
            [FromQuery(Name = "message_data_id")]int messageDataId,
            [FromQuery(Name = "index")]int index,
            [FromQuery(Name = "open_comment")]bool openComment)
        {
            this.weChatMessagePushService.UpdateAlreadyPushArticleComment(mediaId, messageDataId, index, openComment);
            return this.Ok(true);
        }

    }
}
